//
//  CLWKDetailInterfaceController.m
//  chanlin
//
//  Created by Ryan on 15-5-13.
//  Copyright (c) 2015年 chanlin. All rights reserved.
//

#import "CLWKDetailInterfaceController.h"
#import "NSObject+sgr.h"
#import "ChangyanSDK.h"

@interface CLWKDetailInterfaceController()

@property (nonatomic,strong) NSDictionary *dataDic;
@property (nonatomic,strong) NSArray *array;
@property (nonatomic,strong) NSString *count;
@property (nonatomic,strong) NSString *linkType;

@end


@implementation CLWKDetailInterfaceController

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];
    NSString *url=[context sgrGetStringForKey:@"url"];
    self.linkType=[context sgrFGetStringForKey:@"type"];
    if(self.linkType==nil){
        self.linkType=@"0";
    }
    
    [self performSelectorInBackground:@selector(requestData:) withObject:url];
    
    
}

- (void)requestData:(NSString *)strUrl{
    NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:strUrl]];
    
    NSData *data= [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    if(data){
        NSObject *obj=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        if(obj && [obj isKindOfClass:[NSDictionary class]]){
            self.dataDic=[obj sgrGetDictionaryForKey:@"data"];
            [self performSelectorOnMainThread:@selector(updateUI:) withObject:self.dataDic waitUntilDone:NO];
            [self loadComment];
        }
    }
}

- (void)willActivate {
    // This method is called when watch view controller is about to be visible to user
    [super willActivate];
}

- (void)didDeactivate {
    // This method is called when watch view controller is no longer visible
    [super didDeactivate];
}

- (void)updateUI:(NSDictionary *)dic{
    
    [self performSelectorInBackground:@selector(requestHeadImg:) withObject:[dic sgrGetStringForKey:@"newspic"]];
    [self.titleLabel setText:[dic sgrGetStringForKey:@"title"]];
    NSString *befrom=[dic sgrGetStringForKey:@"befrom"];
    if(befrom &&befrom.length>0){
        [self.beFromLabel setText:befrom];
    }
    [self.textLabel setText:[dic sgrGetStringForKey:@"smalltext"]];
}

- (void)requestHeadImg:(NSString *)urlString{
    if(!urlString || urlString.length<5) return;
    UIImage *image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]];
    if(image){
        if(self.head38){
            [self.head38 performSelectorOnMainThread:@selector(setBackgroundImage:) withObject:image waitUntilDone:NO];
        }else if(self.head42){
            [self.head42 performSelectorOnMainThread:@selector(setBackgroundImage:) withObject:image waitUntilDone:NO];
        }
    }

}

- (void)requesetCmtImg:(NSString *)urlString{
    if(!urlString || urlString.length<5) return;
    UIImage *image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]];
    if(image){
        [self.cmtImg performSelectorOnMainThread:@selector(setBackgroundImage:) withObject:image waitUntilDone:NO];
    }
    
}

- (IBAction)colloct:(id)sender{
    NSMutableDictionary *mdic=self.dataDic.mutableCopy;
    [mdic sgrSetObject:self.linkType forKey:@"type"];
    [[self class] openParentApplication:mdic reply:^(NSDictionary *replyInfo, NSError *error) {
        BOOL b=[[replyInfo sgrGetNumberForKey:@"status"] boolValue];
        if(b){
            [self.colloct setTitle:@"收藏成功"];
            [self.colloct performSelector:@selector(setTitle:)
                               withObject:@"在iPhone上收藏本文"
                               afterDelay:1.5];
        }
    }];
}

- (void)updateComment{
    [self.commentLabel setText:self.count];
    NSDictionary *dic=[self.array sgrGetDictionaryForIndex:0];
    if(dic){
        [self.cmtGroup setHidden:NO];
        NSDictionary *pass=[dic sgrGetDictionaryForKey:@"passport"];
        NSString *nickName=[pass sgrGetStringForKey:@"nickname"];
        NSString *location=[pass sgrGetStringForKey:@"ip_location"];
        NSString *str=nickName;
        if(nickName && location){
            str=[NSString stringWithFormat:@"%@ • %@",nickName,location];
        }
        
        [self.cmtUsr setText:str];
       
        [self performSelectorInBackground:@selector(requesetCmtImg:) withObject:[pass sgrGetStringForKey:@"img_url"]];
        [self.cmtContent setText:[dic sgrGetStringForKey:@"content"]];
        
    }
}


- (void)loadComment{
    __weak typeof(self) me=self;
    NSString *title=[self.dataDic sgrGetStringForKey:@"title"];
    NSString *itemId=[self.dataDic sgrGetStringForKey:@"id"];
    
    [ChangyanSDK loadTopic:@""
                topicTitle:title
             topicSourceID:itemId
                  pageSize:@"1"
                   hotSize:@"1"
             completeBlock:^(CYStatusCode statusCode, NSString *responseStr) {
                 
                 if(statusCode==CYSuccess){
                     NSObject *obj=[NSJSONSerialization JSONObjectWithData:[responseStr dataUsingEncoding:NSUTF8StringEncoding]
                                                                   options:0
                                                                     error:nil];
                     NSArray *array=[obj sgrGetArrayForKey:@"comments"];
                     me.array=array;
                     NSString *count=[obj sgrFGetStringForKey:@"cmt_sum"];
                     me.count=count;
                     [me performSelectorOnMainThread:@selector(updateComment) withObject:nil waitUntilDone:NO];
                 }
             }];

}
@end



